package com.litchi.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.litchi.R;
import com.litchi.activity.GoodsListActivity;
import com.litchi.app.MyApplication;
import com.litchi.bean.Brands;
import com.litchi.utils.Constants;
import com.litchi.utils.Utils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class BrandAdapter extends BaseAdapter{

	private MyApplication mApplication;
    private DisplayImageOptions options;

	private Context mContext;
	private List<Brands> list;
	public BrandAdapter(Context context,List<Brands> list){
		mContext=context;
		this.list=list;
		mApplication=(MyApplication)context.getApplicationContext();
		
		 options = new DisplayImageOptions.Builder().bitmapConfig(Bitmap.Config.RGB_565)
		            // •在DisplayImageOptions选项中配置bitmapConfig为Bitmap.Config.RGB_565，因为默认是ARGB_8888，
		            // 使用RGB_565会比使用ARGB_8888少消耗2倍的内存
		            .showImageOnLoading(R.drawable.default_picture)
		            .showImageForEmptyUri(R.drawable.default_picture)
		            .showImageOnFail(R.drawable.default_picture)
		            .cacheInMemory(true)
		            .cacheOnDisc(true)
		            .considerExifParams(false)
		            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
		            .displayer(new RoundedBitmapDisplayer(Utils.convertDipOrPx(context, 3)))
		            .build();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHold mViewHold  ;
		if (convertView==null) {
			mViewHold=new ViewHold();
			convertView=LayoutInflater.from(mContext).inflate(R.layout.brand_item, null);
			mViewHold.brandImageView=(ImageView)convertView.findViewById(R.id.brand_imageView);
			convertView.setTag(mViewHold);
			
		}else{
			mViewHold=(ViewHold)convertView.getTag();
		}
		mViewHold.brandImageView.setTag(position);
		 
		mViewHold.brandImageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int id=(int) v.getTag();
				Intent intent =new Intent(mContext,GoodsListActivity.class);
				intent.putExtra(Constants.BRAND_KEY, list.get(id));
				mContext.startActivity(intent);
			}
		});
        mApplication.imgLoader.displayImage(Constants.HTTP_FILE+list.get(position).getBrandImg().getUrl(), mViewHold.brandImageView );
		
		return convertView;
	}
	
	
	class ViewHold {
		private ImageView brandImageView;
	}

}
